// 处理 "" null 0 undefined
import { Message } from 'element-ui'
export const changeParams = (val) => {
    const _type = ['0', "", null, undefined]
    return !_type.includes(val)
}
/**
 * 处理初是否可以编辑的逻辑处理函数
 * @param {*} row  当前行数据
 * @param {*} item  当前列数据item.prop
 * @param {*} status  综合查询进来的状态status
 */
export function handleEdit(row, item, status, fieldProjectManager) {
    const prop = item.prop || item.property
    let flag = true //不允许编辑
    // 代办的是否可以编辑的逻辑
    if (!status) {
        flag = transactHandleEdit(prop, row, flag, fieldProjectManager)
    } else {
        flag = transactHandleEdit(prop, row, flag, fieldProjectManager)
    }
    return flag
}
const transactHandleEditOne = (prop, row, state, fieldProjectManager) => {
    let flag = state
    const { materialDisableNoStock } = row
    if (prop === "keeper" && changeParams(materialDisableNoStock)) {
        if (fieldProjectManager != "" && fieldProjectManager != null) {
            flag = false;
        }
    }
    return flag
}
const transactHandleEditTwo = (prop, row, state, fieldProjectManager) => {
    let flag = state
    const { materialUsableNoStock } = row
    if (prop === "keeper" && changeParams(materialUsableNoStock)) {
        if (fieldProjectManager != "" && fieldProjectManager != null) {
            flag = false;
        }
    }
    return flag
}
const transactHandleEditThree = (prop, row, state) => {
    let flag = state
    const { isRetreatConfig } = row
    if ((['materialDisableNoStock', 'materialUsableNoStock'].includes(prop)) && isRetreatConfig === '0') {
        flag = false;
    }
    return flag
}
// 代办可以编辑
const transactHandleEdit = (prop, row, state, fieldProjectManager) => {
    let flag = state
    const { mdseNum, materialUsableNoStock, materialDisableNoStock } = row
    //如果商合订单审减数量为0，不可编辑填写 
    if (+mdseNum === 0 && (["materialUsableStock", "materialUsableNoStock", "materialDisableStock", "materialDisableNoStock", "noMaterialDisableNoStock"].includes(prop))) {
        flag = false;
    }
    //如果有实物可用不入库和有实物不可用不入库，保管人、保管电话、保管人地址可以编辑
    if ((["keeper", "keeperContact", "keeperAddress"].includes(prop)) && (!changeParams(materialUsableNoStock)) && (!changeParams(materialDisableNoStock))) {
        flag = false;
    }
    flag = transactHandleEditOne(prop, row, flag, fieldProjectManager)
    flag = transactHandleEditTwo(prop, row, flag, fieldProjectManager)
    flag = transactHandleEditThree(prop, row, flag)
    return flag
}


/**
 * 
 * @param {*} row 当前行数据
 * @param {*} item  当前列数据item.prop
 * @param {*} saveValue 上一次保存的数据
 * @param {*} precision 精度
 */
//保存数据

const handleSaveDataOne = (row, item, fieldProjectManager, fieldProjectManagerName) => {
    if (changeParams(row['materialDisableNoStock'])) {
        if (!['', null].includes(fieldProjectManager)) {
            row['keeper'] = fieldProjectManagerName
        }
    }
    if (changeParams(row['materialUsableNoStock'])) {
        if (!['', null].includes(fieldProjectManager)) {
            row['keeper'] = fieldProjectManagerName
        }
    }
}
const handleSaveDataTwo = (row) => {
    return changeParams(row['materialUsableStock']) && changeParams(row['materialUsableNoStock']) && changeParams(row['materialDisableStock']) && changeParams(row['materialDisableNoStock'])
}
const handleSaveDataThree = (row) => {
    return changeParams(row['noMaterialDisableNoStock']) && changeParams(row['materialUsableNoStock']) && changeParams(row['materialDisableStock']) && changeParams(row['materialDisableNoStock'])

}
const handleSaveDataFour = (row) => {
    return changeParams(row['materialUsableStock']) && changeParams(row['noMaterialDisableNoStock']) && changeParams(row['materialDisableStock']) && changeParams(row['materialDisableNoStock'])
}
const handleSaveDataFive = (row) => {
    return changeParams(row['materialUsableStock']) && changeParams(row['materialUsableNoStock']) && changeParams(row['noMaterialDisableNoStock']) && changeParams(row['materialDisableNoStock'])
}
const handleSaveDataSix = (row) => {
    return changeParams(row['materialUsableStock']) && changeParams(row['materialUsableNoStock']) && changeParams(row['materialDisableStock']) && changeParams(row['noMaterialDisableNoStock'])
}
const handleSaveDataSeven = (row, item, resultmdseNum, _typesProp, enumFunc, saveValue) => {
    if (row['outPutType'] === '25' && row['powerCable'] === '1' && _typesProp.includes(item.prop)) {
        if (+row[item.prop] !== resultmdseNum && (enumFunc[item.prop])) {
            Message({
                message: `采购订单号"${row['orderCode']}"物资为利旧类电缆，仅允许选择一个有无实物下的退库类型将出库单数量整单退库！`,
                type: 'warning'
            })
            row[item.prop] = saveValue
            throw ''
        }
    }
}
export function handleSaveData(row, item, saveValue, precision, fieldProjectManager, fieldProjectManagerName) {
    // 满足利旧物资
    //满足利旧物资且允许调减
    const _typesProp = ["noMaterialDisableNoStock", "materialUsableStock", "materialUsableNoStock", "materialDisableStock", "materialDisableNoStock"]
    const enumFunc = {
        "noMaterialDisableNoStock": handleSaveDataTwo(row),
        "materialUsableStock": handleSaveDataThree(row),
        "materialUsableNoStock": handleSaveDataFour(row),
        "materialDisableStock": handleSaveDataFive(row),
        "materialDisableNoStock": handleSaveDataSix(row)
    }
    const resultmdseNum = Math.abs(row['mdseNum'])
    if (row[item.prop] !== '' && Number(row[item.prop]) !== 0) {
        handleSaveDataSeven(row, item, resultmdseNum, _typesProp, enumFunc, saveValue)
    }
    // 无实物退库物资审减金额
    if (item.prop === "noMaterialDisableNoStock") {
        let num = +row['noMaterialDisableNoStock'];
        row['noMaterialReduceAmount'] = Math.round((row['price'] * num) * 10000) / 10000;
    }
    if (['materialUsableStock', 'materialUsableNoStock', 'materialDisableStock', 'materialDisableNoStock'].includes(item.prop)) {
        let num1 = +row['materialUsableStock'] + (+row['materialUsableNoStock']) + (+row['materialDisableStock']) + (+row['materialDisableNoStock']);
        row['materialReduceAmount'] = Math.round((row['price'] * num1) * 10000) / 10000;
    }
    handleSaveDataOne(row, item, fieldProjectManager, fieldProjectManagerName)
}